package com.biz.primus.ms.promotion.service.promotion;

import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.promotion.exception.PromotionExceptionType;
import com.biz.primus.model.promotion.vo.CutPriceSalePromotionVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.promotion.dao.po.CutPriceSalePromotion;
import com.biz.primus.ms.promotion.dao.repository.CutPriceSalePromotionRepository;
import com.biz.primus.ms.promotion.trans.CutPriceSalePromotionPo2VoMapper;
import com.google.common.collect.Lists;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 降价拍后台管理Service
 *
 * @author david-liu
 */
@Service
public class CutPriceSalePromotionManagementServiceImpl extends AbstractBaseService implements CutPriceSalePromotionManagementService {

    @Autowired
    private CutPriceSalePromotionRepository cutPriceSalePromotionRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCutPriceSalePromotion(CutPriceSalePromotionVo vo) {
        AssertUtils.notNull(vo, PromotionExceptionType.ILLEGAL_PARAMETER);
        CutPriceSalePromotion cutPriceSalePromotion;
        if (Objects.isNull(vo.getId())) {
            cutPriceSalePromotion = new CutPriceSalePromotion();
            cutPriceSalePromotion.setId(idService.getNextId());
        } else {
            cutPriceSalePromotion = cutPriceSalePromotionRepository.findOne(vo.getId());
            AssertUtils.notNull(cutPriceSalePromotion, PromotionExceptionType.ILLEGAL_STATE);
        }
        cutPriceSalePromotion.setName(vo.getName());
        cutPriceSalePromotion.setDescription(vo.getDescription());
        cutPriceSalePromotion.setChannels(vo.getChannels());
        cutPriceSalePromotion.setStartTime(LocalDateTime.from(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").parse(vo.getStartTime())));
        cutPriceSalePromotion.setEndTime(LocalDateTime.from(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").parse(vo.getEndTime())));
        cutPriceSalePromotion.setEffectMemberLevels(Optional.ofNullable(vo.getEffectMemberLevels()).orElse(Lists.newArrayList()).stream().map(Integer::valueOf).collect(Collectors.toList()));
        cutPriceSalePromotion.setAllowCoupon(vo.getAllowCoupon());
        cutPriceSalePromotion.setUserOnceGainLimit(vo.getUserOnceGainLimit());
        cutPriceSalePromotion.setScope(vo.getScope());
        cutPriceSalePromotion.setScopeIds(Arrays.stream(StringUtils.split(vo.getScopeIds(), ",")).map(id -> Long.valueOf(StringUtils.trim(id))).collect(Collectors.toList()));
        cutPriceSalePromotion.setCommonStatus(vo.getCommonStatus());
        cutPriceSalePromotion.setCutPriceStair(vo.getRules().size());
        cutPriceSalePromotion.setCutPriceRule(JsonUtils.obj2Json(vo.getRules()));
        cutPriceSalePromotionRepository.save(cutPriceSalePromotion);
        return true;
    }

    @Override
    public SpringPageVO<CutPriceSalePromotionVo> getCutPriceSalePromotions(PageVo pageVo) {
        Page<CutPriceSalePromotion> page = cutPriceSalePromotionRepository.findAll(pageVo.toPageRequest());
        List<CutPriceSalePromotionVo> voList = new CutPriceSalePromotionPo2VoMapper().mapWithList(page.getContent());
        return new SpringPageVO.Builder<CutPriceSalePromotionVo>().number(page.getNumber()).size(page.getSize()).totalElements(page.getTotalElements()).content(voList).build();
    }

    @Override
    public CutPriceSalePromotionVo getCutPriceSalePromotion(Long promotionId) {
        AssertUtils.notNull(promotionId, PromotionExceptionType.ILLEGAL_PARAMETER);
        CutPriceSalePromotion cutPriceSalePromotion = cutPriceSalePromotionRepository.findOne(promotionId);
        AssertUtils.notNull(cutPriceSalePromotion, PromotionExceptionType.ILLEGAL_STATE);
        return new CutPriceSalePromotionPo2VoMapper().map(cutPriceSalePromotion);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean toggleCutPriceSalePromotionStatus(Long promotionId) {
        AssertUtils.notNull(promotionId, PromotionExceptionType.ILLEGAL_PARAMETER);
        CutPriceSalePromotion cutPriceSalePromotion = cutPriceSalePromotionRepository.findOne(promotionId);
        AssertUtils.notNull(cutPriceSalePromotion, PromotionExceptionType.ILLEGAL_STATE);
        cutPriceSalePromotion.setCommonStatus(CommonStatus.toggle(cutPriceSalePromotion.getCommonStatus()));
        cutPriceSalePromotionRepository.save(cutPriceSalePromotion);
        return true;
    }
}
